package org.fengling.readingplatform.manager.controller;

import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.fengling.readingplatform.manager.service.NovelService;
import org.fengling.readingplatform.model.dto.novel.NovelDto;
import org.fengling.readingplatform.model.entity.novel.Novel;
import org.fengling.readingplatform.model.vo.common.Result;
import org.fengling.readingplatform.model.vo.common.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Tag(name = "小说管理接口")
@RestController
@RequestMapping(value="/admin/novel/novel")
public class NovelController {

    @Autowired
    private NovelService novelService;

    //小说列表方法
    // current: 当前页  limit：每页显示记录数
    // SysRoleDto: 条件角色名称对象
    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(@PathVariable("current") Integer current,
                             @PathVariable("limit") Integer limit,
                             @RequestBody NovelDto novelDto){
        //pageHelper插件实现分页
        PageInfo<Novel> pageInfo =  novelService.findByPage(novelDto, current, limit);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }

    //小说添加的方法
    @PostMapping(value = "/saveNovel")
    public Result saveNovel(@RequestBody Novel novel){
        novelService.save(novel);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //小说修改的方法
    @PutMapping("/updateNovel")
    public Result updateNovel(@RequestBody Novel novel){
        System.out.println(novel);
        novelService.updateNovel(novel);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    //小说删除的方法
    @DeleteMapping("/deleteById/{novelId}")
    public Result deleteById(@PathVariable("novelId") Long id){
        novelService.deleteById(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    //查询小说标号的方法
    @GetMapping("/findNovelId")
    public Result findNovelId(NovelDto novelDto){
        Long novelId = novelService.findNovelId(novelDto);
        System.out.println(novelId);
        return Result.build(novelId, ResultCodeEnum.SUCCESS);
    }
}
